﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class TblStatusJobDAO
    {
        private DBService _db = null;
        public TblStatusJobDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        public DataTable GetJobStatus(int JobID)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblJobStatus where JobID= @JobID order by ID desc",
                    new string[] {"@JobID"},
                    new object [] {JobID }, CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetCurrentStatusJob(int JobID)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_GetCurrentStatus",
                    new string[] { "@JobID" },
                    new object[] { JobID }, CommandType.StoredProcedure);

            }
            catch (Exception ex)
            {

                throw ex;
            }
            return dt;
        }

        public bool JobStatusIns(TblJobStatus JobStatus)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblJobStatus_Ins",
                    new string[] { "@JobID", "@StatusDate", "@StatusJobs", "@Notes" },
                    new object[] { JobStatus.JobID, JobStatus.StatusDate, JobStatus.StatusJobs, JobStatus.Notes},
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable LoadLastStatus(int JobID)
        {
            DataTable dt = null;
            try 
            {
                dt = _db.ExeSP_DataTable("select top 1 * from TblJobStatus where JobID = @JobID order by ID desc",
                    new string[] { "@JobID" },
                    new object[] { JobID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        public bool UpdateQuoteComplete(int JobID, string Status)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("update TblQuotes set QuoteStatus = @Status where JobID = @JobID",
                    new string[] { "@JobID", "@Status" },
                    new object[] { JobID, Status },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public bool UpdateNote(int JobID, string Note, int ID)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("update TblJobStatus set Notes = @Note where JobID = @JobID and ID=@ID",
                    new string[] { "@JobID", "@Note", "@ID" },
                    new object[] { JobID, Note, ID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
    }
}
